package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.common.utils.BeanUtils;
import com.hmall.search.domain.dto.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-11-09 17:01
 **/
@Component
@RequiredArgsConstructor
public class ListenerItem {
    private final ItemClient itemClient;
    private final RestHighLevelClient restHighLevelClient;
    /**
     * 监听商品添加、更新
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.add",durable = "true"),
            exchange = @Exchange(value = "item.direct",type = ExchangeTypes.DIRECT),
            key = "add"))
    public void listenerAddItems(String message){
        updateAndAddItems(message);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.update",durable = "true"),
            exchange = @Exchange(value = "item.direct",type = ExchangeTypes.DIRECT),
            key = "update"))
    public void listenerUpdateItems(String message){
        updateAndAddItems(message);
    }
    /**
     * 监听商品删除
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.del",durable = "true"),
            exchange = @Exchange(value = "item.direct",type = ExchangeTypes.DIRECT),
            key = "del"))
    public void listenerDeleteItems(Long id){
        // 1.准备Request，两个参数，第一个是索引库名，第二个是文档id
        DeleteRequest request = new DeleteRequest("items", id.toString());
        // 2.发送请求
        try {
            restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 添加或更新商品
     * @param message
     */
    public void updateAndAddItems(String message){
        ItemDTO itemDTO = JSONUtil.toBean(message, ItemDTO.class);
        // 1.根据id查询商品数据
        Long id = itemDTO.getId();
        ItemDTO item = itemClient.queryItemById(id);
        // 2.转换为文档类型
        ItemDoc itemDoc = BeanUtils.copyBean(item, ItemDoc.class);
        // 3.将ItemDTO转json
        String doc = JSONUtil.toJsonStr(itemDoc);

        // 1.准备Request对象
        IndexRequest request = new IndexRequest("items").id(itemDoc.getId());
        // 2.准备Json文档
        request.source(doc, XContentType.JSON);
        // 3.发送请求
        try {
            restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
